﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace student
{
    class DBHelper
    {
       
        public static string ConnString = @"Data Source=DESKTOP-HC317IS\MSSQLSERVER2012;Initial Catalog=Schools;Integrated Security=True";
        private static SqlConnection Conn = null;

        private static void InitConnection()
        {
           
            if (Conn == null)
                Conn = new SqlConnection(ConnString);           
            if (Conn.State == ConnectionState.Closed)
                Conn.Open();          
            if (Conn.State == ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }

        public static DataTable GetDataTable(string sqlStr)
        {
            InitConnection();
            DataTable table = new DataTable();
            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
            dap.Fill(table);
            Conn.Close();
            return table;
        }

        public static bool ExecuteNonQuery(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            int result = cmd.ExecuteNonQuery();
            Conn.Close();
            return result > 0;
        }
    }
}
